package com.xiaoke.liantongdemo.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.LinearLayout;

import com.xiaoke.liantongdemo.R;
import com.xiaoke.liantongdemo.bean.BusinessInputs;
import com.xiaoke.liantongdemo.constants.Constants;
import com.xiaoke.liantongdemo.pdf.PDFUtil;
import com.xiaoke.liantongdemo.widget.BusinessInputsLayout;
import com.xiaoke.liantongdemo.xml.XmlUtil;

public class BusinessInputActivity extends Activity {
	
	private int businessId;
	
	private Button confirm;
	private LinearLayout mainLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.business_input);
		confirm = (Button) findViewById(R.id.confirm_bt);
		mainLayout = (LinearLayout) findViewById(R.id.main_layout);
//		FrameLayout mainLayout = new FrameLayout(this);
//		mainLayout.setOrientation(LinearLayout.VERTICAL);
//		mainLayout.setGravity(Gravity.CENTER_HORIZONTAL);
		businessId = getIntent().getIntExtra(Constants.PARAM_NAME_BUSINESS_ID, 0);
		BusinessInputs businessInputs = XmlUtil.getBusinessInputs(businessId);
		final BusinessInputsLayout businessInputsLayout = new BusinessInputsLayout(this, businessInputs);
		LayoutParams  businessInputsParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		businessInputsParams.setMargins(100, 50, 100, 50);
		mainLayout.addView(businessInputsLayout, businessInputsParams);
//		Button btn = new Button(this);
//		btn.setText("确定");
//		btn.setTextSize(size)
		confirm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				BusinessInputs businessInputs = businessInputsLayout.getInputedBusinessInputs();
				new FillPDFTask().execute(businessInputs);
				
			}
		});
//		LayoutParams  buttonParams = new LayoutParams(160, 80);
//		buttonParams.gravity = Gravity.BOTTOM | Gravity.RIGHT;
//		mainLayout.addView(btn, buttonParams);
//		setContentView(mainLayout);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == Constants.RESULT_CODE_MANAGED){
			setResult(Constants.RESULT_CODE_MANAGED);
			finish();
		}
	}
	
	class FillPDFTask extends AsyncTask<BusinessInputs, Integer, Void>{
		
		private ProgressDialog dialog;

		@Override
		protected Void doInBackground(BusinessInputs... params) {
			try {
				PDFUtil.fillPDF(params[0], Constants.DATA_PATH + Constants.BUSINESS_PDF_PATH + businessId + Constants.PDF_SUFFIX, 
						Constants.DATA_PATH + Constants.CREATED_PDF_PATH + Constants.TEMP_FILE_NAME + Constants.PDF_SUFFIX);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			dialog.dismiss();
			Intent intent = new Intent(BusinessInputActivity.this, BusinessConfirmActivity.class);
			intent.putExtra(Constants.PARAM_NAME_BUSINESS_ID, businessId);
			startActivityForResult(intent, Constants.REQUEST_CODE_MANAGE);
		}
		
		@Override
		protected void onPreExecute() {
			dialog = new ProgressDialog(BusinessInputActivity.this);
			dialog.setMessage("正在创建PDF文档 ...");
			dialog.show();
			super.onPreExecute();
		}
		
	}

}
